package com.pic.mgt.monitor.parser.haishin;

import java.text.ParseException;
import java.util.Date;

import com.pic.mgt.monitor.WrongUnitFormatException;
import com.pic.mgt.monitor.parser.TimeParser;
import com.pic.mgt.utils.DateConvertUtils;

public  final class ExecutionTimeParser extends TimeParser {
	// 執行時間字串長度 <<2011/11/02 配信時間格式修改 by Timothy>>
	private final int executionTimeStringLen = 12;
	// 執行時間字串格式 <<2011/11/02  配信時間格式修改 by Timothy>>
	private final String executionTimeStringFormat = "yyyyMMddHHmm";

	@Override
	public Date parse(Object _executionTimeString) throws
										WrongUnitFormatException{
		String executionTimeString = ((String)_executionTimeString).trim();
		
		// 確認長度是否正確
		if (executionTimeString.length() != this.executionTimeStringLen) {
			throw new WrongUnitFormatException("Execution Time '" + executionTimeString + "' is invalid. Due to wrong length.");
		}
		
		Date executionTime;
		try {
			// 確認是否都為數字
			// throws ParseException
			super.checkTimeString(executionTimeString);
			
			executionTime = DateConvertUtils.convertToDate(
								executionTimeString, this.executionTimeStringFormat);
		} catch (StringIndexOutOfBoundsException e) {
			throw new WrongUnitFormatException(
					"Execution Time '" + executionTimeString + "' is invalid.", e);
		} catch (NumberFormatException e) {
			throw new WrongUnitFormatException(
					"Execution Time '" + executionTimeString + "' is invalid.", e);
		} catch (ParseException e) {
			throw new WrongUnitFormatException(
					"Execution Time '" + executionTimeString + "' is invalid.", e);
		}		
		return executionTime;
	}
}
